
window.date = window.d = window.date||{};

(function () {
    //日期时间
    function DateTool(date) {
        //转成双位数
        function toDoubleNum(time) {
            time =  time<10?'0'+time:time;
            return time;
        }

        //星期数组
        function weeks(text) {
            var list = [text+"日",text+"一",text+"二",text+"三",text+"四",text+"五",text+"六"];
            return list;
        }

        //xxxxYearxxMonthxxDay
        function yearMonthDay(year,month,day,isDouble) {
            if (!isDouble) {
                return date.getFullYear()+year+(date.getMonth()+1)+month+date.getDate()+day;
            } else {
                return toDoubleNum(date.getFullYear())+year+toDoubleNum((date.getMonth()+1))+month+toDoubleNum(date.getDate())+day;
            }
        }

        //创建日期对象
        // var date = new Date();
        date = date?new Date(date):new Date();

        //把日期对象转换为时分秒
        function toHMS() {
            return date.getHours()+"："+date.getMinutes()+"："+date.getSeconds();
        }

        //把日期对象转换为双位数的时分秒
        function toHHMMSS() {
            return toDoubleNum(date.getHours())+"："+toDoubleNum(date.getMinutes())+"："+toDoubleNum(date.getSeconds());
        }

        //今天、昨天、三天前
        function dayAgo(time) {
            if (time.getFullYear()<date.getFullYear()||time.getMonth()<date.getMonth()||(date.getDate()-time.getDate())>=2) {
                return "三天前";
            } else if (date.getDate()-time.getDate()==1) {
                return "昨天";
            } else if (date.getDate()-time.getDate()==0){
                return "今天";
            } else {
                return "请输入今天之前的时间"
            }
        }

        //星期一
        function xingQiWeek() {
            // var list = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
            var list = weeks("星期");
            return list[date.getDay()];
        }

        //礼拜一
        function liBaiWeek() {
            var list = weeks("礼拜");
            return list[date.getDay()];
        }

        //周一
        function zhouWeek() {
            var list = weeks("周");
            return list[date.getDay()];
        }

        //上午 下午
        function shangWu() {
            if (date.getHours()<12) {
                return "上午"
            } else {
                return "下午"
            }
        }

        //计算传入的日期 距离当前有多少小时 多少分 多少秒（没有小时 就不显示 没有分 就不显示）
        function timeAgo(time) {
            var seconds = parseInt((date.getTime()-time.getTime())/1000);
            var hour,minute,second;
            if (seconds>=3600) {
                hour = parseInt(seconds/3600);
                minute = parseInt(seconds%3600/60);
                second = seconds%3600%60;
                return hour+"："+minute+"："+second;
            } else if (seconds>=60) {
                minute = parseInt(seconds/60);
                second = seconds%60;
                return minute+"："+second;
            } else {
                return seconds;
            }
        }

        //2018年8月17号 2018年8月17号 08:05:05
        function yearMonthHao() {
            return yearMonthDay("年","月","号",0);
        }

        //2018年8月17日 2018年8月17日 08:05:05
        function yearMonthRi() {
            return yearMonthDay("年","月","日",0);
        }

        //2018-08-05 2018-08-05 08:05:05
        function year_Month_Day() {
            return yearMonthDay("-","-","",1);
        }

        //08/06
        function mmdd() {
            return toDoubleNum(date.getMonth()+1)+"/"+toDoubleNum(date.getDate());
        }

        return {
            hms:toHMS(),
            hhmmss:toHHMMSS(),
            dayAgo:dayAgo,
            xingQiWeek:xingQiWeek(),
            liBaiWeek:liBaiWeek(),
            zhouWeek:zhouWeek(),
            shangWu:shangWu(),
            timeAgo:timeAgo,
            yearMonthHao:yearMonthHao(),
            yearMonthRi:yearMonthRi(),
            year_Month_Day:year_Month_Day(),
            yearMonthHaoTime:yearMonthHao()+" "+toHHMMSS(),
            yearMonthRiTime:yearMonthRi()+" "+toHHMMSS(),
            year_Month_DayTime:year_Month_Day()+" "+toHHMMSS(),
            mmdd:mmdd(),
        }
    }
    window.d.DateTool = DateTool;
    window.date = window.d;
})();

